package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.GameConstants;
import com.real8.romemp.client.place.HireAuxiliaPlace;
import com.real8.romemp.client.place.HireAuxiliaPlace.EMenuSelection;
import com.real8.romemp.client.place.ShipBuildingPlace;
import com.real8.romemp.client.ui.HireAuxiliaView;
import com.real8.romemp.shared.GameData;

public class HireAuxiliaActivity extends AbstractActivity implements
		HireAuxiliaView.Presenter {
	private final ClientFactory clientFactory;
	private EMenuSelection menuOption;
	private HireAuxiliaView view;

	public HireAuxiliaActivity(HireAuxiliaPlace place,
			ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.menuOption = place.getMenuOption();
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getHireAuxiliaView();
		if (view != null) {
			view.setPresenter(this);

			// prepare view
			view.hideAll();

			// show menu
			GameData gameData = clientFactory.getGameData();
			int availableAuxilia = gameData.getMA_auxiliaAvailable();
			int hiredAuxilia = gameData.getMH_auxiliaHired();
			view.showMenu(availableAuxilia, hiredAuxilia);

			// menu selected? => show question
			switch (menuOption) {
			case HIRE:
				view.showQuestion(menuOption);
				break;
			case FIRE:
				view.showQuestion(menuOption);
				break;
			case DO_NOTHING:
				Scheduler.get().scheduleDeferred(new ScheduledCommand() {
					@Override
					public void execute() {
						clientFactory.getPlaceController().goTo(
								new ShipBuildingPlace());
					}
				});
				break;
			}

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	@Override
	public void onSelected(EMenuSelection selection) {
		goToMenu(selection);
	}

	private void goToMenu(EMenuSelection newMenuOption) {
		clientFactory.getPlaceController().goTo(
				new HireAuxiliaPlace(newMenuOption));
	}

	@Override
	public void onAnswer(int amount) {
		GameData gameData = clientFactory.getGameData();
		int availableAuxilia = gameData.getMA_auxiliaAvailable();
		int hiredAuxilia = gameData.getMH_auxiliaHired();

		// check
		if (amount >= 0) {
			switch (menuOption) {
			case HIRE:
				if (amount <= availableAuxilia) {
					// update auxilia
					gameData.setMA_auxiliaAvailable(availableAuxilia - amount);
					gameData.setMH_auxiliaHired(hiredAuxilia + amount);

					// return to menu selection
					goToMenu(EMenuSelection.NONE);
				}
				break;
			case FIRE:
				if (amount <= hiredAuxilia) {
					// update auxilia
					gameData.setMA_auxiliaAvailable(availableAuxilia + amount);
					gameData.setMH_auxiliaHired(hiredAuxilia - amount);

					// rioting
					if (amount > 0) {
						riot(amount);
					} else {
						// return to menu selection
						goToMenu(EMenuSelection.NONE);
					}
				}
				break;
			}
		}
	}

	private void riot(final int amount) {
		if (view != null) {
			// prepare view
			view.hideAll();

			// show riot warning
			view.showRiotWarning();
		}

		// delay
		Timer timer = new Timer() {
			@Override
			public void run() {
				// check riot
				int MI = amount * 20;
				int X = Random.nextInt(100) + 1;
				if (MI < X) {
					// show no riot message
					if (view != null) {
						view.showNoRiotMessage();
					}

					// delay
					Timer timer = new Timer() {
						@Override
						public void run() {
							// return to menu selection
							goToMenu(EMenuSelection.NONE);
						}
					};
					timer.schedule(GameConstants.DELAY_6000);
				} else {
					// calculate loses
					final int lootedGold = 1000 * (Random.nextInt(10) + 1);
					final int killedPopulation = 1000 * (Random.nextInt(20) + 1);
					final int killedWorkers = 2 + (Random.nextInt(10) + 1);

					// update from loses
					GameData gameData = clientFactory.getGameData();
					gameData.setTR_treasury(gameData.getTR_treasury()
							- lootedGold);
					gameData.setPO_population(gameData.getPO_population()
							- killedPopulation);
					gameData.setLS_labourShipBuilders(gameData
							.getLS_labourShipBuilders() - killedWorkers / 3);
					gameData.setLE_labourArtisans(gameData
							.getLE_labourArtisans() - killedWorkers / 3);

					// show riot message
					if (view != null) {
						view.showRiotMessage();
					}

					// delay
					Timer timer = new Timer() {
						@Override
						public void run() {
							// show loses
							if (view != null) {
								view.showRiotLoses(lootedGold,
										killedPopulation, killedWorkers / 3,
										killedWorkers / 3);

								// wait for key press
								view.showContinue();
							}
						}
					};
					timer.schedule(GameConstants.DELAY_6000);
				}
			}
		};
		timer.schedule(GameConstants.DELAY_6000);
	}

	@Override
	public void onContinue() {
		// return to menu selection
		goToMenu(EMenuSelection.NONE);
	}
}
